package com.hockeo.client.ui.presenters;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.google.gwt.user.client.Window;
import com.hockeo.client.VHEventBus;
import com.hockeo.client.ui.views.StatusView;
import com.hockeo.shared.VHUtil;
import com.hockeo.shared.domain.transitory.SessionInfo;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;

/**
 * Presenter for the top status bar.
 *
 * @version $Id$
 * @author jjanke
 */
@Presenter( view = StatusView.class )
public class StatusPresenter extends BasePresenter<StatusView, VHEventBus>
{
  public void onStart()
  {
    this.eventBus.changeTopWidget( this.view.getViewWidget() );
  }

  public void onReceivedSessionInfo( final SessionInfo si )
  {
    this.view.updateUserInfo( si.getConnectedManager() );

    // attach event listener to button
    //
    Button btnLogInOut = this.view.getLogInOutButton();
    if ( !btnLogInOut.hasListeners( Events.Select ) )
    {
      btnLogInOut.addSelectionListener( new SelectionListener<ButtonEvent>() {
        @Override
        public void componentSelected( ButtonEvent evt )
        {
          Window.open( si.getLogInOutUrl() + VHUtil.getUrlSuffix(), "_self", null );
        }
      } );
    }
  }
}
